package com.tpc.control.jpa;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * ReserveHeader entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "reserve_header", catalog = "production_control")
public class ReserveHeader implements java.io.Serializable {

	// Fields

	private String reserveNo;
	private String customer;
	private String customerName;
	private Timestamp createDate;
	private String createBy;
	private Timestamp changeDate;
	private String changeBy;
	private Set<ReserveItem> reserveItems = new HashSet<ReserveItem>(0);

	// Constructors

	/** default constructor */
	public ReserveHeader() {
	}

	/** minimal constructor */
	public ReserveHeader(String reserveNo, String customer,
			String customerName, Timestamp createDate, String createBy) {
		this.reserveNo = reserveNo;
		this.customer = customer;
		this.customerName = customerName;
		this.createDate = createDate;
		this.createBy = createBy;
	}

	/** full constructor */
	public ReserveHeader(String reserveNo, String customer,
			String customerName, Timestamp createDate, String createBy,
			Timestamp changeDate, String changeBy, Set<ReserveItem> reserveItems) {
		this.reserveNo = reserveNo;
		this.customer = customer;
		this.customerName = customerName;
		this.createDate = createDate;
		this.createBy = createBy;
		this.changeDate = changeDate;
		this.changeBy = changeBy;
		this.reserveItems = reserveItems;
	}

	// Property accessors
	@Id
	@Column(name = "reserve_no", unique = true, nullable = false, length = 20)
	public String getReserveNo() {
		return this.reserveNo;
	}

	public void setReserveNo(String reserveNo) {
		this.reserveNo = reserveNo;
	}

	@Column(name = "customer", nullable = false, length = 10)
	public String getCustomer() {
		return this.customer;
	}

	public void setCustomer(String customer) {
		this.customer = customer;
	}

	@Column(name = "customer_name", nullable = false, length = 40)
	public String getCustomerName() {
		return this.customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	@Column(name = "create_date", nullable = false, length = 19)
	public Timestamp getCreateDate() {
		return this.createDate;
	}

	public void setCreateDate(Timestamp createDate) {
		this.createDate = createDate;
	}

	@Column(name = "create_by", nullable = false, length = 20)
	public String getCreateBy() {
		return this.createBy;
	}

	public void setCreateBy(String createBy) {
		this.createBy = createBy;
	}

	@Column(name = "change_date", length = 19)
	public Timestamp getChangeDate() {
		return this.changeDate;
	}

	public void setChangeDate(Timestamp changeDate) {
		this.changeDate = changeDate;
	}

	@Column(name = "change_by", length = 20)
	public String getChangeBy() {
		return this.changeBy;
	}

	public void setChangeBy(String changeBy) {
		this.changeBy = changeBy;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "reserveHeader")
	public Set<ReserveItem> getReserveItems() {
		return this.reserveItems;
	}

	public void setReserveItems(Set<ReserveItem> reserveItems) {
		this.reserveItems = reserveItems;
	}
	
	/* Customize */
	public List<ReserveItem> retrieveItemsAll() {
		//ReserveItemDAO itemDAO = new ReserveItemDAO();
		//List<ReserveItem> dataList = itemDAO.findByHeader(this.getReserveNo());

		List<ReserveItem> dataList = new ArrayList<ReserveItem>(this.getReserveItems());
		Collections.sort(dataList, new ItemSortByLine());
		return dataList;
	}
	
	private class ItemSortByLine implements Comparator<ReserveItem>{
	    public int compare(ReserveItem o1, ReserveItem o2) {
	        return o1.getId().getReserveLine().compareTo(o2.getId().getReserveLine());
	    }
	}

}